import { createRouter, createWebHistory } from 'vue-router'
// import { useUserStore } from '@/stores/user'
import Layout from '@/components/Layout.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false },
  },
  {
    path: '/',
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
      },
      {
        path: 'bookings',
        name: 'Bookings',
        component: () => import('@/views/Bookings.vue'),
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/Profile.vue'),
      },
    ],
  },
  {
    path: '/login-test',
    name: 'LoginTest',
    component: () => import('@/views/LoginStatusTest.vue'),
    meta: { requiresAuth: false },
  },
  {
    path: '/simple-test',
    name: 'SimpleTest',
    component: () => import('@/views/SimpleTest.vue'),
    meta: { requiresAuth: false },
  },
  {
    path: '/hello',
    name: 'Hello',
    component: () => import('@/views/HelloWorld.vue'),
    meta: { requiresAuth: false },
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/',
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 路由守卫 - 暂时禁用以诊断问题
// router.beforeEach(async (to, from, next) => {
//   try {
//     const userStore = useUserStore()
//
//     // 等待加载用户信息
//     await userStore.loadUserFromStorage()
//
//     console.log('🔍 路由守卫检查:', {
//       path: to.path,
//       requiresAuth: to.meta.requiresAuth,
//       isLoggedIn: userStore.isLoggedIn,
//       user: userStore.user
//     })
//
//     if (to.meta.requiresAuth && !userStore.isLoggedIn) {
//       // 需要登录但未登录，跳转到登录页
//       console.log('🚫 需要登录，跳转到登录页')
//       next('/login')
//     } else if (to.path === '/login' && userStore.isLoggedIn) {
//       // 已登录用户访问登录页，跳转到首页
//       console.log('✅ 已登录，跳转到首页')
//       next('/')
//     } else {
//       console.log('✅ 允许访问')
//       next()
//     }
//   } catch (error) {
//     console.error('❌ 路由守卫错误:', error)
//     next()
//   }
// })

export default router
